<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\common\model\Banner;
use app\common\model\Enterprise;
use app\common\model\Company as CompanyModel;

class Company
{
    //程宇
    //获取公司黄页
    public function index()
    {
        return sucessMsg('200', '获取成功', [
            'banner' => $this->banner(),//获取banner
            'enterprise' => $this->enterprise(),//获取公司黄页-企业订单

        ]);
    }

    /**
     * 获取banner
     */
    public function banner()
    {
        $model = New Banner();
        $banner = $model->where(['status' => 1, 'type' => 6])
            ->field('id,title,image')
            ->limit(0, 1)
            ->select();
        foreach ($banner as $v) {
            $v['image'] = env('app.yuming') . $v['image'];
        }
        return $banner;
    }

    /**
     * 获取公司黄页-企业订单
     */
    public function enterprise()
    {
        $model = New Enterprise();
        $enterprise = $model->where('status', 1)
            ->field('id,vip,product,no_vip,online')
            ->order('sort', 'asc')
            ->limit(0, 1)
            ->select();
        foreach ($enterprise as $v) {
            $v['vip'] = env('app.yuming') . $v['vip'];
            $v['product'] = env('app.yuming') . $v['product'];
            $v['no_vip'] = env('app.yuming') . $v['no_vip'];
            $v['online'] = env('app.yuming') . $v['online'];
        }
        return $enterprise;
    }

    /**
     * 获取公司黄页-推荐厂商
     */
    public function company()
    {
        $model = New CompanyModel();
        $page = input('page/d', 1);
        $pagesize = 4;
        $company = $model->where('status', 1)
            ->field('id,name,type,content,image')
            ->order('sort', 'asc')
            ->page($page, $pagesize)
            ->select();
        foreach ($company as $v) {
            $v['image'] = env('app.yuming') . $v['image'];
        }
        $count = count($model->where('status', 1)->select());
        $maxpage = ceil($count / $pagesize);
        return sucessMsg('200', '获取成功',
            ['company' => $company,
                'page' => $page,
                'count' => $count,
                'pagesize' => $pagesize,
                'maxpage' => $maxpage]);
    }

    /**
     * 获取公司黄页-新入厂商
     */
    public function newcompany()
    {
        $model = New CompanyModel();
        $page = input('page/d', 1);
        $pagesize = 4;
        $newcompany = $model->where('status', 1)
            ->field('id,name,type,content,image')
            ->order('id', 'desc')
            ->page($page, $pagesize)
            ->select();
        foreach ($newcompany as $v) {
            $v['image'] = env('app.yuming') . $v['image'];
        }
        $count = count($model->where('status', 1)->select());
        $maxpage = ceil($count / $pagesize);
        return sucessMsg('200', '获取成功',
            ['newcompany' => $newcompany,
                'page' => $page,
                'count' => $count,
                'pagesize' => $pagesize,
                'maxpage' => $maxpage]);
    }
}
